package com.system.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.system.demo.entity.DTO.ApplicationSearchDTO;
import com.system.demo.entity.database.Paper;
import com.system.demo.entity.database.SubmitApplication;
import com.system.demo.entity.excelFile.ApplicationDTO;
import com.system.demo.entity.mailPage.CompetitionWorkVO;
import com.system.demo.mapper.dynamicSqlProvider.ApplicationDynamicSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SubmitApplicationMapper extends BaseMapper<SubmitApplication> {

    /**
     * 根据ID更新状态
     * @param id 记录的主键ID
     * @param status 新的状态值
     */
    @Update("UPDATE submitapplication SET status = #{status} WHERE id = #{id}")
    int updateStatusById(@Param("id") Integer id, @Param("status") Integer status);

    @SelectProvider(type = ApplicationDynamicSqlProvider.class, method = "getCombinedApplicationData")
    List<ApplicationDTO> getCombinedApplicationData(@Param("search") ApplicationSearchDTO search);

    @Update({
            "<script>",
            "UPDATE submitapplication",
            "SET status = #{newStatus}",
            "WHERE id IN",
            "<foreach collection='idList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    int batchUpdateStatus(@Param("idList") List<Integer> idList, @Param("newStatus") Integer newStatus);


    @Delete("DELETE FROM submitapplication WHERE id = #{id} AND uid = #{uid}")
    void deleteById(@Param("id") Integer id, @Param("uid") String uid);

    @Select("SELECT * FROM submitapplication WHERE id = #{id}")
    SubmitApplication findByApplicationId(@Param("id") Integer id);

    @Select("<script>" +
            "SELECT id FROM submitapplication WHERE uid IN " +
            "<foreach item='item' index='index' collection='uids' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<Integer> selectIdsByUids(@Param("uids") List<String> uids);



    //获取当前用户通过认定的竞赛ids
    @Select("SELECT id FROM submitapplication WHERE uid = #{uid} AND status = 5")
    List<Integer> selectIdsByUidAndStatus(@Param("uid") String uid);


    @Select("SELECT " +
            "SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) AS pendingReview, " +
            "SUM(CASE WHEN status = 4 THEN 1 ELSE 0 END) AS toBeRecognized " +
            "FROM submitapplication")
    CompetitionWorkVO selectStatusCounts();
}
